<?php

class Db
{

    public $conn = null;

    public $tableName = '';

    public $selectSql = 'SELECT * FROM __TABLE_NAME__ __WHERE__ __ORDER__';

    public $insertSql = 'INSERT INTO __TABLE_NAME__ SET __FIELDS__';

    public $updateSql = 'UPDATE __TABLE_NAME__ SET __FIELDS__ __WHERE__';

    public $deleteSql = 'DELETE FROM __TABLE_NAME__ __WHERE__';

    public function __construct($tableName)
    {
        $this->tableName = $tableName;
        $config = require __PATH__ . '/config/config.php';
        if ($this->conn == null) {
            $this->conn = mysqli_connect($config['DB_HOST'], $config['DB_USER'], $config['DB_PASS']) or die('连接数据库失败！');
        }
        $this->query('SET NAMES UTF8');
        $this->selectDb($config['DB_NAME']);
        $this->selectSql = str_replace('__TABLE_NAME__', $config['DB_PREFIX'] . $tableName, $this->selectSql);
        $this->insertSql = str_replace('__TABLE_NAME__', $config['DB_PREFIX'] . $tableName, $this->insertSql);
        $this->updateSql = str_replace('__TABLE_NAME__', $config['DB_PREFIX'] . $tableName, $this->updateSql);
        $this->deleteSql = str_replace('__TABLE_NAME__', $config['DB_PREFIX'] . $tableName, $this->deleteSql);
    }

    public function query($sql)
    {
        return mysqli_query($this->conn, $sql);
    }

    public function selectDb($dbName = '')
    {
        mysqli_select_db($this->conn, $dbName) or die('选择数据库失败！');
    }

    public function find()
    {
        $sql = str_replace('__ORDER__', '', $this->selectSql);
        $query = $this->query($sql);
        $row = mysqli_fetch_assoc($query);
        return $row;
    }

    public function limit($string = '')
    {
        return $this;
    }

    public function select()
    {
        $list = array();
        $sql = $this->selectSql;
        $query = $this->query($sql);
        while ($row = mysqli_fetch_assoc($query)) {
            $list[] = $row;
        }
        return $list;
    }

    public function order($string = '')
    {
        if (!empty($string)) {
            $this->selectSql = str_replace('__ORDER__', 'ORDER BY ' . $string, $this->selectSql);
        }
        return $this;
    }

    public function insert($dataList = array())
    {
        $sql = array();
        foreach ($dataList as $key => $value) {
            $sql[] = '`' . $key . '` = "' . $value . '"';
        }
        $sql = str_replace('__FIELDS__', implode(',', $sql), $this->insertSql);
        return $this->query($sql);
    }

    public function where($string = '')
    {
        $this->selectSql = str_replace('__WHERE__', 'WHERE ' . $string, $this->selectSql);
        $this->updateSql = str_replace('__WHERE__', 'WHERE ' . $string, $this->updateSql);
        $this->deleteSql = str_replace('__WHERE__', 'WHERE ' . $string, $this->deleteSql);
        return $this;
    }

    public function update($dataList)
    {
        $sql = array();
        foreach ($dataList as $key => $value) {
            $sql[] = '`' . $key . '` = "' . $value . '"';
        }
        $sql = str_replace('__FIELDS__', implode(',', $sql), $this->updateSql);
        return $this->query($sql);
    }

    public function delete()
    {
        $sql = $this->deleteSql;
        return $this->query($sql);
    }

}